####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Fig A18, A19
###
####################################################################################


library(dplyr)
library(ggplot2)
library(miceadds)
library(AER)
library(rlist)

################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
source(paste0(wd_code, '/05_data_load_clean_absun.R'))



################################################################################
# Plot functions
################################################################################

makeplot_housing = function(model_output){
  temp = model_output
  container = cbind(temp[2:8,], period = c(-4, -3, -2, 0, 1, 2, 3))
  container = rbind(container, c(0, 0, NA, NA, -1)) %>% as.data.frame()
  container = container[order(container$period),]
  colnames(container) = c('beta', 'se', 'tval', 'pval', 'period')
  ggplot(data=container, aes(y=beta, x=factor(period)))+
    geom_point() + 
    geom_errorbar(aes(ymin=beta-1.96*se, ymax=beta+1.96*se), colour="black", width=.1) +
    geom_hline(yintercept=0, colour='red')+
    xlab("Periods to Policy Application")+
    ylab("") +
    theme_light() +
    theme(legend.position="bottom", legend.title = element_blank(), text = element_text(size=18))
}



makeplot_housing_split = function(modelli){
  container = lapply(modelli, function(x) rbind(cbind(x[2:8,], period = c(-4, -3, -2, 0, 1, 2, 3)), c(0,0,NA,NA,-1)))
  container = list.rbind(container) %>% as.data.frame()
  container = cbind(container, group=rep(c('Low Immigration', 'Medium Immigration', 'High Immigration'), each=8))
  colnames(container) = c('beta', 'se', 'tval', 'pval', 'period', 'group')
  container$group.f = factor(container$group, levels = c('Low Immigration', 'Medium Immigration', 'High Immigration'))
  ggplot(data=container, aes(y=beta, x=factor(period), colour=group.f))+
    geom_point(position = position_dodge(width = 0.5)) + 
    geom_errorbar(aes(ymin=beta-1.96*se, ymax=beta+1.96*se), width=.1, position = position_dodge(width = 0.5)) +
    geom_hline(yintercept=0, colour='red')+
    xlab("Periods to Policy Application") + 
    ylab("") +
    theme_light() +
    theme(legend.position="bottom", legend.title = element_blank(), text = element_text(size=18))
}





################################################################################
# Direct policy effects on housing (Figure A18)
################################################################################

# On the number of public housing dwellings
df1 = df_did
model5 = lm(data=df1, num_hlm ~  m4 + m3 + m2 + p0 + p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period))
model5_cl = coeftest(model5, vcov = vcovCL, cluster = ~df1$CODGEO)
makeplot_housing(model5_cl)

ggsave(paste0(wd_res, '/figures/figA18.pdf'), height=8, width=12)


################################################################################
# Direct policy effects on housing, by immigration levels (Figure A19)
################################################################################

modelli = list()
for (n in c(1,2,3)){
  print(paste0('Immigation tercile ', n, ' -------------- '))
  df1 = df_did[which(!is.na(df_did$num_hlm) & df_did$imm_quant_99==n),]
  temp = lm(data=df1, I(num_hlm) ~  m4 + m3 + m2 + p0 + p1 + p2 + p3 + running + 
              factor(CODGEO) + factor(policy_period))
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  
  print(temp[1:10,])
  modelli[[n]] = temp
}

makeplot_housing_split(modelli)
ggsave(paste0(wd_res, '/figures/figA19.pdf'),  height=7, width=10)

